home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / dos / c / ser_lib.exe / SPP.EXE / SERIAL.H < prev    next >
C/C++ Source or Header  |  1992-02-07  |  6KB  |  200 lines

  1. #ifndef _SERIAL_H
  2. #define _SERIAL_H
  3. //============================================================================
  4. //
  5. //  SERIAL.H
  6. //
  7. //  Source include file for class SerialPort
  8. //  Serial++ Library SPP.LIB Ver. 1.01
  9. //
  10. //  Copyright (c) 1991,1992 Cortlandt Technologies, All rights reserved.
  11. //
  12. //     Cortlandt Technologies, P.O. Box 195, Pleasantville NY 10570
  13. //
  14. //  Please direct all inquiries to the above address,
  15. //  or via Compuserve: 71066,155
  16. //
  17. //============================================================================
  18.  
  19. #ifndef __STDLIB_H
  20. #include <stdlib.h>
  21. #endif
  22.  
  23.     enum CommPorts {
  24.                         COM_1,
  25.                         COM_2,
  26.                         CommPorts_Ct
  27.                   };
  28.  
  29.     enum Baud {                            // divisors for selected rates
  30.              B_50      = 2304,
  31.              B_75      = 1536,
  32.              B_110     = 1047,
  33.              B_134_5   = 857,
  34.              B_150     = 768,
  35.                  B_300     = 384,
  36.                  B_600     = 192,
  37.                  B_1200    = 96,
  38.              B_1800    = 64,
  39.              B_2000    = 58,
  40.                  B_2400    = 48,
  41.              B_3600    = 32,
  42.                  B_4800    = 24,
  43.              B_7200    = 16,
  44.                  B_9600    = 12,
  45.                  B_19200   = 6,
  46.              B_38400   = 3,
  47.                  B_57600   = 2,
  48.                  B_115200  = 1
  49.                  };
  50.  
  51.     enum Parity {                  // bit pattern for parity set
  52.                     P_N = 0,           // none
  53.                P_O = 0x0008,      // odd
  54.                     P_E = 0x0018,      // even
  55.                P_M = 0x0028,      // mark (always 1)
  56.                P_S = 0x0038       // space (always 0)
  57.                     };
  58.  
  59.     enum DataBits {                // bit pattern for data bits set
  60.                       D_5 = 0,
  61.                       D_6 = 1,
  62.                       D_7 = 2,
  63.                       D_8 = 3
  64.                       };
  65.  
  66.     enum StopBits  {               // bit pattern for stop bits set
  67.                         S_1,
  68.                         S_2
  69.                         };
  70.  
  71.     enum CommRegs {              // Comm port registers:
  72.                       RBR,           // Receive Buffer
  73.                       THR,           // Transmit Hold
  74.                       DLR,           // Divisor LSB
  75.                       DMR,           // Divisor MSB
  76.                       IER,           // Interrupt Enable
  77.                       IIR,           // Interrupt Id
  78.                       LCR,           // Line Control
  79.                       MCR,           // Modem Control
  80.                       LSR,           // Line Status
  81.                       MSR,           // Modem Status
  82.                       IMR,           // Interrupt Mask
  83.                       CommRegs_Ct    // last enum has value == ct
  84.                       };
  85.  
  86.    enum LSR_Masks {
  87.                   DR       =  0x0001,   // data ready
  88.                   OE       =  0x0002,   // overrun error
  89.                   PE       =  0x0004,   // parity error
  90.                   FE       =  0x0008,   // framing error
  91.                   BI       =  0x0010,   // break indication
  92.                   THRE     =  0x0020,   // THR empty
  93.                   TSRE     =  0x0040    // Transmit Shift empty
  94.                   };
  95.  
  96.    enum MSR_Masks {
  97.                   DCTS     =  0x0001,   // delta clear to send
  98.                   DDSR     =  0x0002,   // delta data set ready
  99.                   TERI     =  0x0004,   // trailing edge ring ind
  100.                   DDCD     =  0x0008,   // delta data carrier detect
  101.                   CTS      =  0x0010,   // clear to send   
  102.                   DSR      =  0x0020,   // data set ready
  103.                   RI       =  0x0040,   // ring indicator
  104.                   DCD      =  0x0080    // data carrier detect
  105.                   };
  106.  
  107.    enum MCR_Masks {
  108.                   DTR      =  0x0001,   // data terminal ready
  109.                   RTS      =  0x0002,   // req to sent
  110.                   OUT1     =  0x0004,   // aux user o/p 1
  111.                   OUT2     =  0x0008,   // aux user o/p 2
  112.                   LOOP     =  0x0010    // enable loopback
  113.                   };
  114.  
  115.  
  116.  
  117.     const size_t SERIALPORT_STD_BUF_SIZE = 2048;
  118.  
  119. //===========================================================================
  120.  
  121.     struct PortAnchor;
  122.  
  123.     class SerialPort
  124.         {
  125.         private:
  126.  
  127.             PortAnchor                 *pa;
  128.  
  129.             Baud                    speed;
  130.             DataBits                data_bits;
  131.             StopBits                stop_bits;
  132.             Parity                  parity;
  133.             CommPorts               c_port;
  134.  
  135.             void open(size_t b_size);
  136.  
  137.             static void close(PortAnchor *anc);
  138.          static void breakCheck(void);
  139.  
  140.         public:
  141.  
  142.             SerialPort(CommPorts  cp,
  143.                           Baud       sp=B_1200,
  144.                           DataBits   db=D_8,
  145.                           Parity     ps=P_N,
  146.                           StopBits   sb=S_1,
  147.                           size_t          b_size=SERIALPORT_STD_BUF_SIZE,
  148.                     int        brk_off = 0);
  149.  
  150.             virtual ~SerialPort(void);
  151.  
  152. //       input routines
  153.             virtual int inbyte(unsigned char &cc, unsigned int timeout=0);
  154.             virtual void unbyte(unsigned char cc);
  155.  
  156.             virtual size_t inmem(void *mp,
  157.                                         size_t msize,
  158.                                         unsigned int timeout=0);
  159.  
  160.             virtual char *instr(char *buf,
  161.                                       size_t maxlen,
  162.                                       unsigned int timeout=0);
  163.  
  164. //       output routines
  165.             virtual void outbyte(unsigned char cc);
  166.  
  167.             virtual void outmem(const void *mp, size_t msize);
  168.  
  169.             virtual void outstr(const char *buf);
  170.  
  171. //       line status and control routines
  172.          int getLSR(LSR_Masks mm);
  173.  
  174.          int getMSR(MSR_Masks mm);
  175.  
  176.             int setMCR(MCR_Masks mm, int state);
  177.  
  178.             void sendBreak(int dur=750);
  179.  
  180. //       SerialPort status routines
  181.             int lineOK(void);
  182.  
  183.             int isOwner(void);
  184.  
  185.             Baud getSpeed(void);
  186.  
  187.             DataBits getDataBits(void);
  188.  
  189.             StopBits getStopBits(void);
  190.  
  191.             Parity getParity(void);
  192.  
  193.             int getBreakOff(void);
  194.  
  195.             CommPorts getCommPort(void);
  196.  
  197.     };
  198.  
  199. #endif
  200.